package top.heliming.config;

import com.alibaba.druid.pool.DruidDataSource;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * description: //TODO
 *
 * @author: heliming
 * @date:2019/05/22 上午 12:33
 */
/*
第一种
@Configuration
@PropertySource("classpath:jdbc.properties")
创建 jdbc.properties 文件配置
  jdbc.driverClassName=com.mysql.jdbc.Driver
      jdbc.url=jdbc:mysql://127.0.0.1:3306/leyou
      jdbc.username=root
      jdbc.password=123456*/
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {

  /*@Value("${jdbc.url}")
  String url;
  @Value("${jdbc.driverClassName}")
  String driverClassName;
  @Value("${jdbc.username}")
  String username;
  @Value("${jdbc.password}")
  String password;

  @Bean
  public DataSource dataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(url);
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    return dataSource;
  }*/

  /**③声明有@Bean的方法参数注入*/
  @Bean
  public DataSource dataSource(JdbcProperties jdbc) {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(jdbc.getUrl());
    dataSource.setDriverClassName(jdbc.getDriverClassName());
    dataSource.setUsername(jdbc.getUsername());
    dataSource.setPassword(jdbc.getPassword());
    return dataSource;
  }

 /*
 ①@Autowired注入
 @Autowired
  private JdbcProperties prop;
*/
 /*②构造函数注入
 private JdbcProperties prop;

  public JdbcConfig(JdbcProperties prop) {
    this.prop = prop;
  }*/

 /*
 如果一段属性只有一个Bean需要使用可以在一个类中这样注入jdbcproperties类和上边的都不用写了
 @Configuration
  public class JdbcConfig {

    @Bean
// 声明要注入的属性前缀，SpringBoot会自动把相关属性通过set方法注入到DataSource中
    @ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource() {
      DruidDataSource dataSource = new DruidDataSource();
      return dataSource;
    }
  }*/
}
